package com.IntelligentCommunity.community.controller;

import com.IntelligentCommunity.common.core.domain.R;
import com.IntelligentCommunity.common.core.utils.poi.ExcelUtil;
import com.IntelligentCommunity.common.core.web.controller.BaseController;
import com.IntelligentCommunity.common.core.web.domain.AjaxResult;
import com.IntelligentCommunity.common.core.web.page.TableDataInfo;
import com.IntelligentCommunity.common.log.annotation.Log;
import com.IntelligentCommunity.common.log.enums.BusinessType;
import com.IntelligentCommunity.common.security.annotation.RequiresPermissions;
import com.IntelligentCommunity.community.domain.RealEstate;
import com.IntelligentCommunity.community.request.EstateBathInsertRequest;
import com.IntelligentCommunity.community.service.IRealEstateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实有房屋Controller
 * @author IntelligentCommunity
 * @date 2024-07-21
 */
@RestController
@RequestMapping("/estate")
public class RealEstateController extends BaseController
{
    @Autowired
    private IRealEstateService realEstateService;

    /**
     * 查询实有房屋列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RealEstate realEstate){
        startPage();
        List<RealEstate> list = realEstateService.selectRealEstateList(realEstate);
        return getDataTable(list);
    }

    /**
     * 根据人口id查询实有房屋列表
     */
    @GetMapping("/estateList")
    public AjaxResult list(@RequestParam("id") Integer populationId){
        List<RealEstate> list = realEstateService.selectRealEstateListByPopulationId(populationId);
        return AjaxResult.success(list);
    }


    /**
     * 获取实有房屋详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return success(realEstateService.selectRealEstateById(id));
    }

    /**
     * 新增实有房屋
     */
    @RequiresPermissions("community:estate:add")
    @Log(title = "实有房屋", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RealEstate realEstate){
        return toAjax(realEstateService.insertRealEstate(realEstate));
    }

    /**
     * 修改实有房屋
     */
    @Log(title = "实有房屋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RealEstate realEstate){
        return toAjax(realEstateService.updateRealEstate(realEstate));
    }

    /**
     * 删除实有房屋
     */
    @Log(title = "实有房屋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(realEstateService.deleteRealEstateByIds(ids));
    }

    /**
     * 导出实有房屋列表
     */
    @Log(title = "实有房屋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RealEstate realEstate){
        List<RealEstate> list = realEstateService.selectRealEstateList(realEstate);
        ExcelUtil<RealEstate> util = new ExcelUtil<RealEstate>(RealEstate.class);
        util.exportExcel(response, list, "实有房屋数据");
    }

    /**
     * 添加楼栋
     * @param estateBathInsertRequest
     * @return
     */
    @PostMapping("/bathInsert")
    public R bathInsert(@RequestBody EstateBathInsertRequest estateBathInsertRequest)
    {
        realEstateService.bathInsert(estateBathInsertRequest);
    	return R.ok();
    }
}
